package com.example.day3;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.Spanned;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MainActivity extends AppCompatActivity {
    RadioGroup rg;
    EditText edit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rg = findViewById(R.id.rg);
        edit= findViewById(R.id.edit1);
        edit.setFilters(new InputFilter[]{typeFilter});
        RadioButton rb1 = new RadioButton(this);
        rb1.setText("aaaaaaa");
        RadioButton rb2 = new RadioButton(this);
        rb2.setText("bbbbbbb");
        RadioButton rb3 = new RadioButton(this);
        rb3.setText("cccccccccccc");
        rg.addView(rb1);
        rg.addView(rb2);
        rg.addView(rb3);
    }
    InputFilter typeFilter = new InputFilter() {
        /**
         * @param source 输入的文字
         * @param start 输入-0，删除-0
         * @param end 输入-source文字的长度，删除-0
         * @param dest 原先显示的内容
         * @param dstart 输入-原光标位置，删除-光标删除结束位置
         * @param dend  输入-原光标位置，删除-光标删除开始位置
         * @return
         */
        @Override
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
            Pattern p = Pattern.compile("[\u4e00-\u9fa5]+");
            Matcher m = p.matcher(source.toString());
            if (!m.matches()) return "";
            return null;
        }
    };
}
